package com.xian.im.web.modules.document;

import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.result.RestResult;
import com.xian.im.common.result.RestResultBuilder;
import com.xian.im.common.validator.ValidatorUtils;
import com.xian.im.common.validator.group.AddGroup;
import com.xian.im.modules.im.controller.model.login.SignReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @program: xian-code
 * @description:
 * @author: liru.xian
 * @create: 2021-01-23 14:40
 **/
@RestController
@RequestMapping("im/doc")
@Api(value = "IM 文档", tags = {"IM 文档"})
public class IMController {


    @Value("${xim.org.encryptedKey}")
    private String encryptedKey ;

    @ApiOperation(value = "创建连接", httpMethod = "GET",
            notes = "ws创建连接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appKey", value = "AK系统下发",
                    paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sign", value = "系统生成",
                    paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "account", value = "当前用户账号",
                    paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "channel", value = "渠道来源 对应数据字典type channel",
                    paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "version", value = "版本",
                    paramType = "account", required = true, dataType = "String"),
            @ApiImplicitParam(name = "deviceId", value = "设备ID",
                    paramType = "account", required = true, dataType = "String")
    })
    @RequestMapping("/{appKey}/{sign}/{account}/{channel}/{version}/{deviceId}")
    public RestResult establishConnection(@PathVariable("appKey")String appKey,
                                          @PathVariable("sign")String sign,
                                          @PathVariable("account")String account,
                                          @PathVariable("channel") String channel,
                                          @PathVariable("version")String version,
                                          @PathVariable("deviceId")String deviceId){
        return null;
    }
    @ApiOperation(value = "请求对象", httpMethod = "POST",
            notes = "ws通讯请求对象")
    @RequestMapping(value = "/request/info",method = RequestMethod.POST)
    public RestResult<MsgRequest> connection(@RequestBody MsgRequest request){
        return RestResultBuilder.builder().success(request).build();
    }
    @ApiOperation(value = "返回对象", httpMethod = "POST",
            notes = "ws通讯返回对象")
    @RequestMapping(value = "/response/info",method = RequestMethod.POST)
    public RestResult<MsgResponse> resInfo(@RequestBody MsgResponse response){
        return RestResultBuilder.builder().success(response).build();
    }
    @ApiOperation(value = "生成签名 建立连接的sign", httpMethod = "POST",
            notes = "生成签名")
    @RequestMapping(value = "/sign",method = RequestMethod.POST)
    public RestResult<String> getSign(@RequestBody SignReq signReq){
        ValidatorUtils.validateEntity(signReq, AddGroup.class);
        StringBuilder sb = new StringBuilder();
        sb.append(signReq.getAppKey()).append(signReq.getAccount()).append(signReq.getChannel()).append(signReq.getVersion()).append(signReq.getDeviceId());
        String sign =  MD5Encoder.encode((sb.toString() + encryptedKey).getBytes());
        return RestResultBuilder.builder().success(sign).build();
    }
    @ApiOperation(value = "获取签名串", httpMethod = "GET",
            notes = "获取签名串")
    @RequestMapping(value = "/encryptedKey",method = RequestMethod.GET)
    public RestResult<String> encryptedKey(){
        return RestResultBuilder.builder().success(encryptedKey).build();
    }

}
